﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;
using System.Drawing.Imaging;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using CERT.PageLibrary.Base;


namespace CERT.AcceptanceTests
{
    [Binding]
    public class LoginInternalSteps : TestFixtureBase
    {
        // For additional details on SpecFlow step definitions see http://go.specflow.org/doc-stepdef
        //code taken from http://www.infoq.com/articles/Spec-Flow
        //http://anoopjshetty.wordpress.com/2012/04/19/behaviour-driven-development-bdd-in-net-using-specflow-selenium-and-nunit/


        //scenario One

        [Given(@"I am at the login page")]
        public void GivenIAmAtTheLoginPage()
        {
            var info = ScenarioContext.Current.ScenarioInfo;
            Assert.AreEqual("Logging in with valid credentials", info.Title);
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            CurrentDriver.Navigate().GoToUrl("http://localhost:6014/");

            Assert.AreEqual(2, CurrentDriver.FindElementsByName("UserName").Count);

        }

        [When(@"I fill in the following form")]
        public void WhenIFillInTheFollowingForm(Table table)
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            foreach (var row in table.Rows)
            {
                var textField = CurrentDriver.FindElementByName(row["field"]);

                if (textField == null) Assert.Fail("Expected to find a text field with the name of '{0}'.", row["field"]);
                textField.SendKeys(row["value"]);
            }
        }

        [When(@"I click the login button")]
        public void WhenIClickTheLoginButton()
        {
            if (CurrentDriver == null)
                CurrentDriver = (RemoteWebDriver)ScenarioContext.Current["CurrentDriver"];
            var loginButton = CurrentDriver.FindElementByName("Login");

            if (loginButton == null)
                Assert.Fail("Expected to find a button with the value of 'Login'.");

            loginButton.Click();



        }

        [Then(@"I should be at the home page")]
        public void ThenIShouldBeAtTheHomePage()
        {
            //note the values don't sync.  although CurrentDriver does show the hash
            //doesn't return it to string.
            //string url = CurrentDriver.Url;
            //Uri expected_url = new Uri("http://localhost:6014/#/");

            var navBar = CurrentDriver.FindElementById("main-content");
            if (navBar == null)
                Assert.Fail("Expected to find the navbar");


        }


    }
}
